/**
 * 问题提报 - 编辑页面
 * @author liuw
 * @date 2019/10/25
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: {
                ESAB001_NAME:null,
                ESAB003_NAME:null,
                ESAB014_NAME:null,
                ESAB025_NAME:null,
                ESAB026_NAME:null
            },
            rules: {
                ESAB001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAB002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAB003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAB004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                ESAB007: [
                    {required: true, message: '不可为空', trigger: 'blur'},
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
                ],
                ESAB014: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAB015: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAB025: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAB029: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
            },
            selectOptions:{
                sys_y_n : app.datas_('sys_n_y'),
                esab002Options:app.datas_('problem_esai120_esab002'), // 项目状态
                esab008Options:[], // 区域
                esab009Options:[], // 省
                esab010Options:[], // 市
                esab015Options:app.datas_('problem_esai120_esab015'), // 数据库
                esab014Options:[], // 产品线
                esab029Options:app.datas_('problem_esai100_esaa006'), // 产品线
            }
        },
        callObj,
        pageVue,
        moduleName,
        layOpenIndex
    ;

    var esai120Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'esai120_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'crm/esai120/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        // callObj.update(result.xdata);
                        app.pageModule('esai120').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'crm/esai120/add',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('esai120').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = tagData.layOpenIndex;
            moduleName = tagData.moduleName;
            if (tagData.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {

                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['ESAB_T'],vueData.form);
            pageVue = new Vue({
                el: '#esai120_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    openEsab003:function() { // 主要联系人
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysuser_id',
                                    columnList: [
                                        {label: '人员编号', prop: 'ID', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'},
                                        {label: '账户', prop: 'ACCOUNT', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.ESAB003 = data.ID;
                                    vueData.form.ESAB003_NAME = data.NAME;
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择主要联系人',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openEsab014:function() { // 选择产品线
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwde003',
                                    searchParam:{
                                        GWDE002 : "产品线"
                                    },
                                    columnList: [
                                        {label: '选项值', prop: 'GWDE003', width: '150'},
                                        {label: '选项名称', prop: 'GWDE004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.ESAB014 = data.GWDE003;
                                    vueData.form.ESAB014_NAME = data.GWDE004
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择ERP营运中心',
                                area: ['600px', '470px']
                            }
                        });
                    },

                    openTsc:function(type) { // 维保人员
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysuser_id',
                                    columnList: [
                                        {label: '人员编号', prop: 'ID', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'},
                                        {label: '账户', prop: 'ACCOUNT', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    if(type === "ESAB025"){
                                        vueData.form.ESAB025 = data.ID;
                                        vueData.form.ESAB025_NAME = data.NAME;
                                    }

                                    if(type === "ESAB026"){
                                        vueData.form.ESAB026 = data.ID;
                                        vueData.form.ESAB026_NAME = data.NAME;
                                    }
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择维保人员',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    changeEsab008:function(val) {
                        var obj = {};
                        obj = vueData.selectOptions.esab008Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.ESAB008 = obj.GWAD002; // 区域编号
                        vueData.form.ESAB008_NAME = obj.GWADL004; // 区域名称
                        pageVue.form.ESAB009 = "";  // 省
                        pageVue.form.ESAB010 = "";  // 市
                        vueData.selectOptions.esab009Options = [];
                        app.ajax({
                            xurl: 'crm/esai120/getGwadList',
                            xdata: {GWAD001: '4', GWAD003: vueData.form.ESAB008, type: '3'},
                            cb: function (result) {
                                vueData.selectOptions.esab009Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeEsab009:function(val) {
                        var obj = {};
                        obj = vueData.selectOptions.esab009Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.ESAB009 = obj.GWAD002; // 区域编号
                        vueData.form.ESAB009_NAME = obj.GWADL004; // 区域名称
                        pageVue.form.ESAB010 = "";  // 市
                        vueData.selectOptions.esab010Options = [];
                        app.ajax({
                            xurl: 'crm/esai120/getGwadList',
                            xdata: {GWAD001: '5', GWAD003: vueData.form.ESAB009, type: '4'},
                            cb: function (result) {
                                vueData.selectOptions.esab010Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeEsab010:function(val) {
                        var obj = {};
                        obj = vueData.selectOptions.esab010Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.ESAB010 = obj.GWAD002; // 区域编号
                        vueData.form.ESAB010_NAME = obj.GWADL004; // 区域名称
                    },

                },
                watch: {
                },
                created: function () {
                    self._setEleIsReady('esai120_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
            app.ajax({
                xurl: 'crm/esai120/getGwadList',
                xdata: {GWAD001: '3', TYPE: '2'},
                cb: function (result) {
                    vueData.selectOptions.esab008Options.pushArray(result.data.data);
                }
            });
            app.ajax({
                xurl: 'crm/esai120/getGwadList',
                xdata: {GWAD001: '4', TYPE: '3',GWAD003: vueData.form.ESAB008},
                cb: function (result) {
                    vueData.selectOptions.esab009Options.pushArray(result.data.data);
                }
            });

        }
    });

    return new esai120Edit();

});